local Lua__bysy = fk.CreateSkill {
  name = "Lua__bysy",
  tags = { Skill.Compulsory, },
}

Fk:loadTranslationTable {
  ["Lua__bysy"] = "心斋坐忘",
  ["$Lua__bysy1"] = "抱元守一",
  ["$Lua__bysy2"] = "叮~（防御）",
  [":Lua__bysy"] = "锁定技，当你即将受到致命伤害时，若你的武将牌正面朝上，你防止之并将武将牌背面朝上；"..
  "伤害牌对你生效时，若你的武将牌背面朝上，你随机弃置一张牌并回复2点体力且将溢出的部分改为获得护甲。",
}

Lua__bysy:addEffect(fk.DetermineDamageInflicted, {
  anim_type = "defensive",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(Lua__bysy.name) or target ~= player then return false end
    return player.faceup and player.hp <= data.damage
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("Lua__bysy", 2)
    room:notifySkillInvoked(player, Lua__bysy.name, "defensive")
    player:turnOver()
    data:preventDamage()
  end,
})
Lua__bysy:addEffect(fk.CardEffecting, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(Lua__bysy.name) or target ~= player then return false end
    return not player.faceup and data.card and data.card.is_damage_card and not player:isNude()
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = player:getCardIds("he")
    local to_discard = cards[math.random(1, #cards)]
    room:throwCard(to_discard, Lua__bysy.name, player, player)
    -- 回复体力并转化为护甲
    local recover_num = math.min(2, player.maxHp - player.hp)
    if recover_num > 0 then
      room:recover({
        who = player,
        num = recover_num,
        recoverBy = player,
        skillName = Lua__bysy.name
      })
    end
    local armor_num = math.max(0, 2 - recover_num)
    if armor_num > 0 then
      room:changeShield(player, armor_num)
    end
  end,
})

return Lua__bysy
